<?php

// +----------------------------------------------------------------------
// | Easyadmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议  https://mit-license.org 
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------

namespace app\admin\controller\apply;

use app\admin\model\website\Curriculum as CurriculumModel;
use app\admin\model\website\Lecturer;

use app\common\controller\AdminController;
use app\models\system\SystemWebsite;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use broadcast\redis\setRedis;
/**
 * Class Admin
 * @package app\admin\controller\user
 * @ControllerAnnotation(title="站点管理")
 */
class Curriculum extends AdminController
{

    use \app\admin\traits\Curd;

    protected $sort = [
        'id'   => 'desc',
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new CurriculumModel();
    }



    public function thisimage ()
    {   
        $website = SystemWebsite::where("website_ident",session("website"))->find();
        
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $website->curriculum_image = $post['curriculum_image'];
            $website->curriculum_image_moblie = $post['curriculum_image_moblie'];
            setRedis::setCache(session("website"),'info',$website->toArray(),0);
            $save = $website->save();
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $this->assign("image",$website['curriculum_image']);
        $this->assign("imageMoblie",$website['curriculum_image_moblie']);
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
         //获取网站信息  start
        $website = setRedis::getCache(session("website"),'info');
        if (empty($website)) {
            $website = SystemWebsite::where("website_ident",session("website"))->find();
            if ($website) $website = $website->toArray();
            setRedis::setCache(session("website"),"info",$website,0);
        }
        if (isset($website['curriculum_arrange_pattern'])) {
            if ($website['curriculum_arrange_pattern'] == '2') {
                return $this->redirect('thisimage');
            }
        }

        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            
            $count = $this->model
                ->where($where)
                ->count();
            $list = $this->model
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select()->each(function ($item) {
                    $item['monday'] = json_decode($item['monday'],true);
                    $item['tuesday'] = json_decode($item['tuesday'],true);
                    $item['wednesday'] = json_decode($item['wednesday'],true);
                    $item['thursday'] = json_decode($item['thursday'],true);
                    $item['friday'] = json_decode($item['friday'],true);
                    $item['saturday'] = json_decode($item['saturday'],true);
                    $item['sunday'] = json_decode($item['sunday'],true);

                    if ($item['monday']['name'] == '') {
                        $item['monday_content'] = <<<HTML
                        <p>讲师：暂无<p>
                        <p>直播内容：{$item['monday']['content']}<p>
HTML;
                    }else{
                        $item['monday_content'] = <<<HTML
                        <p>讲师：{$item['monday']['name']}<p>
                        <p>直播内容：{$item['monday']['content']}<p>
HTML;
                    }

                    if ($item['tuesday']['name'] == '') {
                        $item['tuesday_content'] = <<<HTML
                        <p>讲师：暂无<p>
                        <p>直播内容：{$item['tuesday']['content']}<p>
HTML;
                    }else{
                        $item['tuesday_content'] = <<<HTML
                        <p>讲师：{$item['tuesday']['name']}<p>
                        <p>直播内容：{$item['tuesday']['content']}<p>
HTML;
                    }

                    if ($item['wednesday']['name'] == '') {
                        $item['wednesday_content'] = <<<HTML
                        <p>讲师：暂无<p>
                        <p>直播内容：{$item['wednesday']['content']}<p>
HTML;
                    }else{
                        $item['wednesday_content'] = <<<HTML
                        <p>讲师：{$item['wednesday']['name']}<p>
                        <p>直播内容：{$item['wednesday']['content']}<p>
HTML;
                    }
                    if ($item['thursday']['name'] == '') {
                        $item['thursday_content'] = <<<HTML
                        <p>讲师：暂无<p>
                        <p>直播内容：{$item['thursday']['content']}<p>
HTML;
                    }else{
                        $item['thursday_content'] = <<<HTML
                        <p>讲师：{$item['thursday']['name']}<p>
                        <p>直播内容：{$item['thursday']['content']}<p>
HTML;
                    }
                    if ($item['friday']['name'] == '') {
                        $item['friday_content'] = <<<HTML
                        <p>讲师：暂无<p>
                        <p>直播内容：{$item['friday']['content']}<p>
HTML;
                    }else{
                        $item['friday_content'] = <<<HTML
                        <p>讲师：{$item['friday']['name']}<p>
                        <p>直播内容：{$item['friday']['content']}<p>
HTML;
                    }
                    if ($item['saturday']['name'] == '') {
                        $item['saturday_content'] = <<<HTML
                        <p>讲师：暂无<p>
                        <p>直播内容：{$item['saturday']['content']}<p>
HTML;
                    }else{
                        $item['saturday_content'] = <<<HTML
                        <p>讲师：{$item['saturday']['name']}<p>
                        <p>直播内容：{$item['saturday']['content']}<p>
HTML;
                    }
                    if ($item['sunday']['name'] == '') {
                        $item['sunday_content'] = <<<HTML
                        <p>讲师：暂无<p>
                        <p>直播内容：{$item['sunday']['content']}<p>
HTML;
                    }else{
                        $item['sunday_content'] = <<<HTML
                        <p>讲师：{$item['sunday']['name']}<p>
                        <p>直播内容：{$item['sunday']['content']}<p>
HTML;
                    }


                }); 
            
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }





    /**
     * @NodeAnotation(title="添加")
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->post();

            $post['monday'] = json_encode($post['monday']);
            $post['tuesday'] = json_encode($post['tuesday']);
            $post['wednesday'] = json_encode($post['wednesday']);
            $post['thursday'] = json_encode($post['thursday']);
            $post['friday'] = json_encode($post['friday']);
            $post['saturday'] = json_encode($post['saturday']);
            $post['sunday'] = json_encode($post['sunday']);

            try {
                $save = $this->model->save($post);
                $week = $this->model->order("start_time asc")->select();
                if ($week) {
                    $week = $week->toArray();
                    setRedis::setCache(session("website"),"CurriculumList",$week,0);
                }
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }

        $lecturer = Lecturer::column('name');
        $this->assign("lecturer",$lecturer);
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit($id)
    {
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $post['monday'] = json_encode($post['monday']);
            $post['tuesday'] = json_encode($post['tuesday']);
            $post['wednesday'] = json_encode($post['wednesday']);
            $post['thursday'] = json_encode($post['thursday']);
            $post['friday'] = json_encode($post['friday']);
            $post['saturday'] = json_encode($post['saturday']);
            $post['sunday'] = json_encode($post['sunday']);

            try {
                $save = $row->save($post);
                $week = $this->model->order("start_time asc")->select();
                if ($week) {
                    $week = $week->toArray();
                    setRedis::setCache(session("website"),"CurriculumList",$week,0);
                }
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }

        $row->monday = json_decode($row->monday,true);
        $row->tuesday = json_decode($row->tuesday,true);
        $row->wednesday = json_decode($row->wednesday,true);
        $row->thursday = json_decode($row->thursday,true);
        $row->friday = json_decode($row->friday,true);
        $row->saturday = json_decode($row->saturday,true);
        $row->sunday = json_decode($row->sunday,true);
        $lecturer = Lecturer::column('name');
        $this->assign("lecturer",$lecturer);
        $this->assign('row', $row);
        return $this->fetch();
    }
    /**
     * @NodeAnotation(title="删除")
     */
    public function delete($id)
    {
        $row = $this->model->whereIn('id', $id)->select();
        $row->isEmpty() && $this->error('数据不存在');
        
        try {
            $save = $row->delete();
            $week = $this->model->order("start_time asc")->select();
            if ($week) {
                $week = $week->toArray();
                setRedis::setCache(session("website"),"CurriculumList",$week,0);
            }else{
                setRedis::delCache(session('website'),"CurriculumList");
            }
        } catch (\Exception $e) {
            $this->error('删除失败');
        }
        $save ? $this->success('删除成功') : $this->error('删除失败');
    }

}
